//修改首页登录
//index.js
/**
 * getRecommendToday  今日推荐
 * getAdvList  广告
 */
import { getRecommendToday, getAdvList, getMode } from '../../utils/api';
import Toast from '../../miniprogram_npm/vant-weapp/toast/toast';
import Notify from '../../miniprogram_npm/vant-weapp/notify/notify';
//获取应用实例
const app = getApp();

Page({
	data: {
		imgUrls: [
			'../../image/headerAdv.png'
		],
		modelList: [
			// {
			// 	id: '1',
			// 	icon: '../../image/videoIcon.png',
			// 	name: '视频广场',
			// 	type: 'video'
			// },{
			// 	id: '2',
			// 	icon: '../../image/crazySmailIcon.png',
			// 	name: '爆笑段子',
			// 	type: 'jokes'
			// },{
			// 	id: '3',
			// 	icon: '../../image/photoIcon.png',
			// 	name: '精美图片',
			// 	type: 'image'
			// },{
			// 	id: '4',
			// 	icon: '../../image/todayNewIcon.png',
			// 	name: '今日海报',
			// 	type: 'bill'
			// }
		],
		recommendedList: [],
		advObj: null
	},
	toGetUserInfor() {
		const userInfo = wx.getStorageSync('userInfo')
		const userId = wx.getStorageSync('userId')
		if (userInfo && userId) {
			app.globalData.userInfo = userInfo;
			app.globalData.uid = userId;
		}else{
			// 获取用户信息
			wx.getSetting({
				success: res => {
					if (res.authSetting['scope.userInfo']) {
						// 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
						wx.getUserInfo({
							success: res => {
								// 可以将 res 发送给后台解码出 unionId
								app.globalData.userInfo = res.userInfo
								wx.setStorageSync('userInfo', res.userInfo)
								// 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
								// 所以此处加入 callback 以防止这种情况
								if (this.userInfoReadyCallback) {
									this.userInfoReadyCallback(res)
								}
							},
							fail: () => {
								app.globalData.userInfo = null;
								wx.redirectTo({
									url: '/pages/login/index'
								})
							}
						})
					}
					else{
						wx.redirectTo({
							url: '/pages/login/index'
						})
					}
				}
			})
		}
	},
	// 跳转详情页
	toDetailsPage(event) {
		const id = event.currentTarget.dataset.videoid;
		const type = event.currentTarget.dataset.type;
		wx.navigateTo({
			url: `../videoDetails/videoDetails?id=${id}&type=${type}`
		})
	},
	// 获取首页导航分类
	toGetMode() {
		getMode().then((res)=>{
			if (res.code === 200) {
				this.setData({
					modelList: res.data
				})
			} else {
				this.errorToast(res.msg, 'error');
			}
		}).catch(e => {
			this.errorToast('服务异常', 'error');
		})
	},
	// 获取今日推荐
	toGetRecommendToday() {
		Toast.loading({
			mask: true,
			duration: 0,
			mask: false,
			forbidClick: true,
			message: '加载中...'
		});
		getRecommendToday().then((res)=>{
			Toast.clear();
			wx.stopPullDownRefresh();
			if(res.code === 200){
				res.data.map(item=>{
					item.updated_at = item.updated_at.split(' ')[0]
				})
				this.setData({
					recommendedList: res.data
				})
			}else{
				this.errorToast(res.msg,'error');
			}
		}).catch(e=>{
			Toast.clear();
			this.errorToast('服务异常','error');
		})
	},
	// 获取广告信息
	toGetAdvList() {
		getAdvList().then((res)=>{
			if(res.code === 200){
				this.setData({
					advObj: res.data
				})
				app.globalData.advObj = res.data;
			}else{
				this.errorToast(res.msg,'error');
			}
		}).catch(e=>{
			Toast.clear();
			this.errorToast('服务异常','error');
		})
	},
	// 跳转广告详情
	toAdvDetails(event) {
		const img = event.currentTarget.dataset.img;
		const url = event.currentTarget.dataset.url;
		wx.navigateTo({
			url: `../advDetails/index?img=${img}&url=${url}`
		})
	},
	toSquarePage(event) {
		const pageType = event.currentTarget.dataset.type;
		switch(pageType){
			case 'video':
				wx.navigateTo({
					url: `../mediaSquare/index?pageType=2`
				})
				break;
			case 'jokes':
				wx.navigateTo({
					url: `../mediaSquare/index?pageType=1`
				})
				break;
			case 'image':
				wx.navigateTo({
					url: `../mediaSquare/index?pageType=3`
				})
				break;
			case 'bill':
				// this.errorToast('敬请期待','warn');
				wx.navigateTo({
					url: `../newspaperToday/index`
				})
				break;
		}
	},
	errorToast(text, type) {
		switch(type) {
			case 'success':
				Notify({
					text: text,
					color: '#ffffff',
					backgroundColor: '#008000'
				});
				break;
			case 'error':
				Notify({
					text: text,
					color: '#ffffff',
					backgroundColor: '#FF0000'
				});
				break;
			case 'warn':
				Notify({
					text: text,
					color: '#ffffff',
					backgroundColor: '#FFA500'
				});
				break;
		}
	},
	onLoad() {
		// this.toGetUserInfor();
    	this.toGetMode();
		this.toGetRecommendToday();
		this.toGetAdvList();
	},
	// 下拉刷新
	onPullDownRefresh() {
		this.toGetMode();
		this.toGetRecommendToday();
		this.toGetAdvList();
	}
})
